home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / updates / update19.zoo / test / tprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-28  |  3.9 KB  |  133 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "ansidecl.h"
  20. #ifdef    BSD
  21. #include </usr/include/stdio.h>
  22. #define EXIT_SUCCESS 0
  23. #else
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #endif
  27.  
  28.  
  29. void
  30. DEFUN(fmtchk, (fmt), CONST char *fmt)
  31. {
  32.   (void) fputs(fmt, stdout);
  33.   (void) printf(":\t`");
  34.   (void) printf(fmt, 0x12);
  35.   (void) printf("'\n");
  36. }
  37.  
  38. void
  39. DEFUN(fmtst1chk, (fmt), CONST char *fmt)
  40. {
  41.   (void) fputs(fmt, stdout);
  42.   (void) printf(":\t`");
  43.   (void) printf(fmt, 4, 0x12);
  44.   (void) printf("'\n");
  45. }
  46.  
  47. void
  48. DEFUN(fmtst2chk, (fmt), CONST char *fmt)
  49. {
  50.   (void) fputs(fmt, stdout);
  51.   (void) printf(":\t`");
  52.   (void) printf(fmt, 4, 4, 0x12);
  53.   (void) printf("'\n");
  54. }
  55.  
  56. int
  57. DEFUN_VOID(main)
  58. {
  59.   static char shortstr[] = "Hi, Z.";
  60.   static char longstr[] = "Good morning, Doctor Chandra.  This is Hal.  \
  61. I am ready for my first lesson today.";
  62.  
  63.   fmtchk("%.4x");
  64.   fmtchk("%04x");
  65.   fmtchk("%4.4x");
  66.   fmtchk("%04.4x");
  67.   fmtchk("%4.3x");
  68.   fmtchk("%04.3x");
  69.  
  70.   fmtst1chk("%.*x");
  71.   fmtst1chk("%0*x");
  72.   fmtst2chk("%*.*x");
  73.   fmtst2chk("%0*.*x");
  74.  
  75. #ifndef    BSD
  76.   printf("bad format:\t\"%z\"\n");
  77.   printf("nil pointer:\t\"%p\"\n", (PTR) NULL);
  78. #endif
  79.  
  80.   printf("decimal negative:\t\"%d\"\n", -2345);
  81.   printf("octal negative:\t\"%o\"\n", -2345);
  82.   printf("hex negative:\t\"%x\"\n", -2345);
  83.   printf("long decimal number:\t\"%ld\"\n", -123456);
  84.   printf("long octal negative:\t\"%lo\"\n", -2345L);
  85.   printf("long unsigned decimal number:\t\"%lu\"\n", -123456);
  86.   printf("zero-padded LDN:\t\"%010ld\"\n", -123456);
  87.   printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
  88.   printf("space-padded LDN:\t\"%10ld\"\n", -123456);
  89.   printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456);
  90.  
  91.   printf("zero-padded string:\t\"%010s\"\n", shortstr);
  92.   printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
  93.   printf("space-padded string:\t\"%10s\"\n", shortstr);
  94.   printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
  95.   printf("null string:\t\"%s\"\n", (char *)NULL);
  96.   printf("limited string:\t\"%.22s\"\n", longstr);
  97.  
  98.   printf("e-style >= 1:\t\"%e\"\n", 12.34);
  99.   printf("e-style >= .1:\t\"%e\"\n", 0.1234);
  100.   printf("e-style < .1:\t\"%e\"\n", 0.001234);
  101.   printf("e-style big:\t\"%.60e\"\n", 1e20);
  102.   printf ("e-style == .1:\t\"%e\"\n", 0.1);
  103.   printf("f-style >= 1:\t\"%f\"\n", 12.34);
  104.   printf("f-style >= .1:\t\"%f\"\n", 0.1234);
  105.   printf("f-style < .1:\t\"%f\"\n", 0.001234);
  106.   printf("g-style >= 1:\t\"%g\"\n", 12.34);
  107.   printf("g-style >= .1:\t\"%g\"\n", 0.1234);
  108.   printf("g-style < .1:\t\"%g\"\n", 0.001234);
  109.   printf("g-style big:\t\"%.60g\"\n", 1e20);
  110.  
  111. #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
  112.   printf (FORMAT, 0.0, 0.0, 0.0);
  113.   printf (FORMAT, 1.0, 1.0, 1.0);
  114.   printf (FORMAT, -1.0, -1.0, -1.0);
  115.   printf (FORMAT, 100.0, 100.0, 100.0);
  116.   printf (FORMAT, 1000.0, 1000.0, 1000.0);
  117.   printf (FORMAT, 10000.0, 10000.0, 10000.0);
  118.   printf (FORMAT, 12345.0, 12345.0, 12345.0);
  119.   printf (FORMAT, 100000.0, 100000.0, 100000.0);
  120.   printf (FORMAT, 123456.0, 123456.0, 123456.0);
  121. #undef    FORMAT
  122.  
  123. #if __USE_GNU /* GLIBC extension */
  124.   {
  125.     char buf[20];
  126.     printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
  127.         snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
  128.   }
  129. #endif
  130.  
  131.   return(EXIT_SUCCESS);
  132. }
  133.